package org.example.util;

import org.example.entity.User;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class UserDataGenerator {
    private static final Random RANDOM = new Random();
    private static final String BASE_USERNAME = "user_";
    private static final String BASE_EMAIL = "user_%d@example.com";
    // 手机号前缀（国内号段）
    private static final String[] PHONE_PREFIXES = {
            "130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
            "150", "151", "152", "153", "155", "156", "157", "158", "159",
            "170", "171", "173", "176", "177", "178",
            "180", "181", "182", "183", "184", "185", "186", "187", "188", "189"
    };
    // 性别选项（0男 1女 2未知）
    private static final String[] SEX_OPTIONS = {"0", "1", "2"};
    // 账号状态（0正常 1停用）
    private static final String[] STATUS_OPTIONS = {"0", "1"};
    // 随机头像地址
    private static final String[] AVATAR_URLS = {
            "https://example.com/avatars/1.png",
            "https://example.com/avatars/2.png",
            "https://example.com/avatars/3.png"
    };
    // 随机备注
    private static final String[] REMARKS = {
            "普通用户", "VIP会员", "测试账号", "系统生成", ""
    };

    /**
     * 生成指定数量的用户数据（分批返回，避免OOM）
     * @param total 总数量
     * @param batchSize 每批数量
     * @return 迭代器，每次返回一批数据
     */
    public static Iterable<List<User>> generateUsers(long total, int batchSize) {
        return () -> new java.util.Iterator<>() {
            private long generated = 0;

            @Override
            public boolean hasNext() {
                return generated < total;
            }

            @Override
            public List<User> next() {
                int currentBatchSize = (int) Math.min(batchSize, total - generated);
                List<User> batch = new ArrayList<>(currentBatchSize);

                // 生成批次基准时间（30天内随机时间）
                LocalDateTime baseTime = LocalDateTime.now()
                        .minusDays(RANDOM.nextInt(30))
                        .minusHours(RANDOM.nextInt(24))
                        .minusMinutes(RANDOM.nextInt(60));

                for (int i = 0; i < currentBatchSize; i++) {
                    long id = generated + 1;
                    User user = new User();

                    // 基础信息
                    user.setId(id);
                    user.setUsername(BASE_USERNAME + id);
                    user.setAge(RANDOM.nextInt(18, 60)); // 18-59岁
                    user.setEmail(String.format(BASE_EMAIL, id));

                    // 扩展信息
                    user.setPhonenumber(generatePhoneNumber()); // 11位手机号
                    user.setSex(SEX_OPTIONS[RANDOM.nextInt(SEX_OPTIONS.length)]); // 随机性别
                    user.setAvatar(AVATAR_URLS[RANDOM.nextInt(AVATAR_URLS.length)]); // 随机头像
                    user.setPassword("default123"); // 示例密码（实际应加密）
                    user.setStatus(STATUS_OPTIONS[RANDOM.nextInt(STATUS_OPTIONS.length)]); // 随机状态
                    user.setDelFlag("0"); // 默认未删除
                    user.setLoginIp(generateRandomIp()); // 随机IP
                    user.setLoginDate(baseTime.plusHours(RANDOM.nextInt(12))); // 登录时间（基准时间后12小时内）

                    // 审计信息
                    user.setCreateBy("system");
                    user.setCreateTime(baseTime);
                    user.setUpdateBy("system");
                    user.setUpdateTime(baseTime.plusMinutes(RANDOM.nextInt(180))); // 更新时间（创建后3小时内）
                    user.setRemark(REMARKS[RANDOM.nextInt(REMARKS.length)]); // 随机备注

                    batch.add(user);
                    generated++;
                }
                return batch;
            }
        };
    }

    /**
     * 生成随机手机号
     */
    private static String generatePhoneNumber() {
        String prefix = PHONE_PREFIXES[RANDOM.nextInt(PHONE_PREFIXES.length)];
        StringBuilder suffix = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            suffix.append(RANDOM.nextInt(10));
        }
        return prefix + suffix;
    }

    /**
     * 生成随机IP地址
     */
    private static String generateRandomIp() {
        return String.format("%d.%d.%d.%d",
                RANDOM.nextInt(256),
                RANDOM.nextInt(256),
                RANDOM.nextInt(256),
                RANDOM.nextInt(256)
        );
    }
}